package com.codedisaster.steamworks;

import java.nio.Buffer;

abstract class SteamInterface {

	protected long callback;

	SteamInterface() {
		this(0L);
	}

	SteamInterface(long callback) {
		this.callback = callback;
	}

	void setCallback(long callback) {
		this.callback = callback;
	}

	public void dispose() {
		deleteCallback(callback);
	}

	void checkBuffer(Buffer buffer) throws SteamException {
		if (!buffer.isDirect()) {
			throw new SteamException("Direct buffer required.");
		}
	}

	void checkArray(byte[] array, int length) throws SteamException {
		if (array.length < length) {
			throw new SteamException("Array too small, " + array.length + " found but " + length + " expected.");
		}
	}

	/*
		The native API allows passing NULL char pointers in some places, but
		the wrapper code generated by jnigen doesn't check for null Strings.
		So we silently convert to an empty string here, and check again before
		calling the native function.
	 */
	static String maybeNull(String argument) {
		return (argument != null) ? argument : "";
	}

	/*JNI
		#include "SteamCallbackAdapter.h"
	*/

	protected static native void deleteCallback(long callback); /*
		delete (SteamCallbackAdapter*) callback;
	*/

}
